<?php
namespace app\api\controller;

use app\admin\model\User;
use app\admin\model\UserVideo;
use app\api\model\RegionGroupRelatioin;
use app\api\model\UserIdentity;
use app\api\model\Video;
use think\cache\driver\Memcache;
use think\cache\driver\Memcached;
use think\Controller;
use think\Image;
use think\Db;

class Test extends Basic
{
    /**
     * 加密UID
     * Date: 2019/1/16
     * Time: 16:28
     * USER:GCQ
     */
    public function name()
    {
           $name=input('name','');
           json_success(encode($name));
    }

    /**
     * 初始化方法，所有子类继承
     */
    protected function initialize()
    {
        parent::initialize();
    }
    //生成时间
    public function t()
    {
//       halt( Config('PAY_WEIXIN'));
        echo dirname(__FILE__).'<br/>';
        echo dirname(dirname(__FILE__)).'<br/>';
        echo dirname(dirname(dirname(__FILE__))).'<br/>';
        echo dirname(dirname(dirname(dirname(__FILE__)))).'<br/>';
        echo dirname(dirname(dirname(dirname(dirname(__FILE__))))).'<br/>';
//        echo __FILE__;
////        $now = date('Y-m-d H:i:s',time());
//
//      echo   strtotime('2019-2-1',time()).'<br\>';
//      echo  date( 'Y-m-d',strtotime('2019-2-1',time())).'<br\>';
//      echo  date( 'Y-m-d',strtotime('2019-2-1',time())-time()+strtotime("+1years",time())).'<br\>';
////       echo date("Y-m-d H:i:s",strtotime("+1years",strtotime($now))).'<br/>';
//       echo date("Y-m-d H:i:s",strtotime("+6months",strtotime($now))).'半年<br/>';
//
//       echo strtotime("+1years",strtotime($now)).'一年后<br/>';
//
//       echo strtotime("+6months",time()).'半年后';
    }



    /**
     * 统计-平台所有用户，累积拉新用户数量；
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     *
     */
    public function userPullCount(){
        //实例化数据库
        $userPull = Db('UserPull');
        $user = Db('User');

        //查询出所有用户
        $userList = $user->where(array('total_new_user'=>array('neq',0)))->select();
        if($userList){
            foreach ($userList as $k=>$v){
                $count = $userPull->where(array('is_register'=>1,'pid'=>$v['id']))->count();
                $user->where(array('id'=>$v['id']))->update(array('total_register_user'=>$count));
            }
        }
    }

    /**
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 同步用户领取大礼包的姓名及手机
     */
    public function userGift(){
        //实例化数据库
        $user_gift = Db('UserGift');
        $user_identity = Db('UserIdentity');

        $list = $user_gift->where(array('nature'=>array('neq',2)))->select();
        if($list){
            foreach ($list as $k=>$v){
                $info = $user_identity->where(array('user_id'=>$v['user_id']))->field('real_name,mobile')->find();
                if($info) {
                    $user_gift->where(array('id' => $v['id']))->update(array('name' => $info['real_name'], 'mobile' => $info['mobile']));
                }
            }
            echo '请求成功';
        }
    }

    /**
     * 插入form_id
     */
    public function add_Form_Id()
    {
        $user = Input('get.user_id');
        $form_id = Input('get.form_id');
        if (!$user) {
            return json_fail('获取用户id错误');
        }
        $user_id = decode($user);
//        if ($form_id == 'the formId is a mock one') {
//            json_fail([
//                'status' => '-1',
//                'msg' => '开发环境不录入'
//            ]);
//        }
        if (!$form_id) {
            return json_fail('获取form_id错误');
        }
        $user_formid = Db('userFormid');
        $user_formid->insertGetId(array('user_id' => $user_id, 'form_id' => $form_id, 'create_time' => time(), 'expiry_time' => time() + 7 * 24 * 60 * 60));

    }


    /**
     * 发送-课程优惠券码
     * @author jihaichuan
     */
    public function test_send_msm()
    {
//        $mobile = '18611615969';
//        $mobile = '18612534550';
//        $str = '恭喜您已成功获取600元优惠券，打开手机淘宝-＞搜索熊猫钢琴陪练-＞点击首行店铺进店-＞点击右下角‘联系客服’-＞回复‘钢琴盛典’即可领取';
//        $res = http_send_message($mobile, $str);
//        dd($res);
    }


    /**
     * 修复用户ID
     * @author jihaichuan
     */
    public function repair_user_video()
    {
        $row = 0;
        $UserVideo = new UserVideo();
        $video_list = UserVideo::where('user_id', 0)->where('is_first_trial', 1)->field('id,user_id,user_identity_id')->select();
        if ($video_list) {
            foreach ($video_list as $k=>$v) {
                $user_id = UserIdentity::where('id', $v['user_identity_id'])->value('user_id');
                if ($user_id) {
                    $row += $UserVideo->save(array('user_id'=>$user_id), ['id'=> $v['id']]);
//                    echo $UserVideo->getLastSql();exit;
                } else {
                    echo 'user_identity_id='.$v['user_identity_id'].'<br />';
                }
            }
        }
        echo $row.'行数';
    }


    /**
     * 加密用户ID
     * @param $user_id
     * @author jihaichuan
     */
    public function encode_user($user_id, $type=1)
    {
        if ($type == 1) {
            echo encode($user_id);
        } else {
            echo decode($user_id);
        }
    }


    /**
     * 获取token
     * @return mixed
     * @author jihaichuan
     */
    public function get_token()
    {
        return Db('wx_token')->where('wxatid', config('xiaocx_app_id'))->value('access_token');
    }


    /**
     * 修复用户-重复数据
     * @author jihaichuan
     */
    public function repair_user()
    {
        // 1. 查询目前有两条及两条以上的用户，查询主线用户是哪些；
        $user_list = \app\api\model\User::group('openid')->field('id,authorization,real_name,card_no,mobile,openid,is_register,status')->having('count(`openid`) = 2')->limit(10)->select();
        dd($user_list);
    }


    /**
     * 修复用户-注册所属的赛区组
     * @author jihaichuan
     */
    public function repair_region_group_id()
    {
        $user_identity_list = UserIdentity::where('region_id', 'neq', 0)->where('region_group_id', 'eq', 0)->field('id,region_id')->select();
        if ($user_identity_list) {
            foreach ($user_identity_list as $k=>$v) {
                $region_group_id = RegionGroupRelatioin::where('region_id', $v['region_id'])->value('region_group_id');
                UserIdentity::where('id', $v['id'])->update(array('region_group_id'=>$region_group_id));
            }
        }
        dd($user_identity_list);
    }


    /**
     * 修复-用户绑定微信用户后平台内，进行审核再未提交的状态下；
     * @author jihaichuan
     */
    public function repair_user_identity_status()
    {
        $user_identity_id = \app\api\model\UserVideo::where('is_first_trial', 1)->column('user_identity_id');
        $row = UserIdentity::where('id', 'in', $user_identity_id)->update(array('status'=>1));
        dd($row);
    }


    public function aa()
    {
        echo urlsafe_b64decode('5rKh5pyJ5aSq5pma55qE5byA5aeL77yM5LiN5aaC5bCx5LuO5LuK5aSp6KGM5Yqo44CC5oC75pyJ5LiA5aSp77yM6YKj5Liq5LiA54K55LiA54K55Y-v6KeB55qE5pyq5p2l77yM5Lya5Zyo5L2g5b-D6YeM77yM5Lmf5Zyo5L2g55qE6ISa5LiL5oWi5oWi5riF6YCP44CC');

    }


    public function bb()
    {
        $this->getFromId(639210);
    }

    //清空缓存
    public function test_clear()
    {
        $Memcached = new Memcached();
        $Memcached->clear();
//        $A=123;
//        $Memcached = new Memcached();
//        $Memcached->set('zgz','123456');
//        echo $Memcached->get('zgz'.$A);
        //echo phpinfo();
       // cache("carlosk_test","hello");
       // echo(cache("carlosk_test"));
    }


    public function cc()
    {
//        phpinfo();
        $uid = Input('user_id');
        $user_id = decode($uid);
        $name = 'user_id'.$user_id;
//        $this->add_memcache($name, $uid);
        $is_check = $this->check_user_memcache($user_id);
        dd($is_check);
    }

    public function encodeUserId(){
        $user_id = input('user_id');
        echo encode($user_id);
    }
    public function decodeUserId(){
        $user_id = input('user_id');
        echo decode($user_id);
    }
}